package com.badibadi.mytools;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

/** Sharepreference操作帮助类 */

public class SharePrefHelper {
	private Context context;
	private String sName;

	public SharePrefHelper(Context context, String sName) {
		this.context = context;
		this.sName = sName;
	}

	public String getSharePre(String sKey) {
		String str;
		SharedPreferences s = context.getSharedPreferences(sName, 0);
		str = s.getString(sKey, "");
		return str;
	}

	public float getSharePreFloat(String sKey) {
		float str;
		SharedPreferences s = context.getSharedPreferences(sName, 0);
		str = s.getFloat(sKey, 0);
		return str;
	}
	
	public boolean getSharePreBoolean(String key){
		SharedPreferences s = context.getSharedPreferences(sName, 0);
		return s.getBoolean(key, false);
	}

	public int getSharePreInt(String sKey) {
		int str;
		SharedPreferences s = context.getSharedPreferences(sName, 0);
		str = s.getInt(sKey, -1);
		return str;
	}

	public boolean setSharePre(String sKey, String sValue) {
		SharedPreferences s = context.getSharedPreferences(sName, 0);
		Editor editor = s.edit();
		editor.putString(sKey, sValue);
		boolean isSuccess=editor.commit();
//		   
		return isSuccess;
	}

	public boolean setSharePre(String sKey, boolean sValue) {
		
//		JsonUtil.getListByJsonString(strJson, beanClass);
		SharedPreferences s = context.getSharedPreferences(sName, 0);
		Editor editor = s.edit();
		editor.putBoolean(sKey, sValue);
		boolean isSuccess=editor.commit();
		return isSuccess;
		
	}

	public boolean setSharePre(String sKey, float sValue) {
		SharedPreferences s = context.getSharedPreferences(sName, 0);
		Editor editor = s.edit();
		editor.putFloat(sKey, sValue);
		boolean isSuccess=editor.commit();
		return isSuccess;
	}

	public boolean setSharePre(String sKey, int sValue) {
		SharedPreferences s = context.getSharedPreferences(sName, 0);
		Editor editor = s.edit();
		editor.putInt(sKey, sValue);
		boolean isSuccess=editor.commit();
		return isSuccess;
	}

	public void setSharePre(String sKey, long sValue) {
		SharedPreferences s = context.getSharedPreferences(sName, 0);
		Editor editor = s.edit();
		editor.putLong(sKey, sValue);
		editor.commit();
	}
	  /**
     * 检查key是否存在
     * @param key
     * @return
     */
	public boolean isContains(String key)
	{
		SharedPreferences sharedPreferences = context.getSharedPreferences(sName, 0);
		if(sharedPreferences.contains(key))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
